package com.tms.app.tms.ui.home.me;

import com.alibaba.sdk.android.ams.common.util.StringUtil;
import com.tms.app.tms.app.Constant;
import com.tms.app.tms.bean.UpdateInfo;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.UserInfoRes;
import com.tms.app.tms.ui.base.presenter.AbsServicexUtils;
import com.tms.app.tms.ui.base.presenter.BaseListPresenter;
import com.tms.app.tms.ui.base.ui.BaseListFragmnet;
import com.tms.app.tms.utils.AES;
import com.tms.app.tms.utils.ImageUtil;

import org.xutils.http.RequestParams;

import java.io.File;

import static com.tms.app.tms.app.Constant.BD_token;

/**
 * Created by Administrator on 2017/11/27 0027.
 */

public class UserInfoPresenter extends BaseListPresenter {
    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        BaseResponse baseResponse=gson.fromJson(result,BaseResponse.class);
        UserInfoRes userInfoRes=gson.fromJson(result, UserInfoRes.class);
        if (requestType==1){
            userInfoRes.setRequestType(requestType);
            sendMsgToView(userInfoRes);
            iView.refreshComplite();
            return;
        }
        if (requestType==2){//更新用户信息
            userInfoRes.setRequestType(requestType);
            sendMsgToView(userInfoRes);
            return;
        }
        if (requestType==3){// 更新主车
            userInfoRes.setRequestType(requestType);
            sendMsgToView(userInfoRes);
            return;
        }
        if (requestType==4){//更新挂车
            userInfoRes.setRequestType(requestType);
            sendMsgToView(userInfoRes);
            return;
        }
        sendMsgToView(userInfoRes);


    }

    public void getUserInfo(String token){
        getUserInfo(token,AbsServicexUtils.showAndHideLoading);
    }


    public void getUserInfo(String token,String loadingStatus){
        requestType=1;
        this.token=token;
        RequestParams params=new RequestParams(Constant.USER_INFO);
        params.addBodyParameter("token",token);
        params.addBodyParameter("showLoading", loadingStatus);
        getObjectFromServer(params);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getUserInfo(token);
    }
    /*******
     * 更新用户信息
     * @param userInfo
     * @param
     */
    public void updateUserInfo(UserInfo userInfo){
        requestType=2;
        RequestParams params=new RequestParams(Constant.USER_UPDATE);
        //params.setAsJsonContent(true);
        params.setMultipart(true);
        params.addBodyParameter("mobile",userInfo.getMobile());
        params.addBodyParameter("address",userInfo.getAddress());
        params.addBodyParameter("driverName",userInfo.getDriverName());
        params.addBodyParameter("idNumber",userInfo.getIdNumber());
        if (!StringUtil.isEmpty(userInfo.idcardA))
            params.addBodyParameter("idcardA",new File(userInfo.idcardA));
        if (!StringUtil.isEmpty(userInfo.idcardB))
            params.addBodyParameter("idcardB",new File(userInfo.idcardB));
        if (!StringUtil.isEmpty(userInfo.driver))
            params.addBodyParameter("driver",new File(userInfo.driver));
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);

    }
    public void updateCar(UserInfo userInfo,int requestType){
        if (requestType==0){
            updateHeadCar(userInfo);
        }else {
            updateTrailer(userInfo);
        }
    }
    private void updateHeadCar(UserInfo userInfo){
        requestType=3;
        RequestParams params=new RequestParams(Constant.UPDATE_CAR);
        //params.setAsJsonContent(true);
        params.setMultipart(true);
        params.addBodyParameter("carLicense",userInfo.getCarLicense());
        params.addBodyParameter("brand",userInfo.getHead_brand());
        params.addBodyParameter("buyTime",userInfo.getHead_buy_time()+"");
        params.addBodyParameter("serviceLife",userInfo.getHead_service_life());
        params.addBodyParameter("licenseImg",userInfo.getHead_license_img());
        if (!StringUtil.isEmpty(userInfo.moveCardA))
            params.addBodyParameter("file",new File(userInfo.moveCardA));
        if (userInfo.getTrailerId()>0)
            params.addBodyParameter("trailerId",userInfo.getTrailerId()+"");
        params.addBodyParameter("driverId",userInfo.getId()+"");
        if (!StringUtil.isEmpty(String.valueOf(userInfo.getHeadstockId())))
            params.addBodyParameter("id",userInfo.getHeadstockId()+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);
    }

    /*******
     * 更新挂车
     * @param userInfo
     */
    private void updateTrailer(UserInfo userInfo){
        requestType=4;
        RequestParams params=new RequestParams(Constant.UPDATE_TRAILER);
        //params.setAsJsonContent(true);
        params.setMultipart(true);
        params.addBodyParameter("trailerNumber",userInfo.getTrailerNumber());
        params.addBodyParameter("brand",userInfo.getTrailer_brand());
        params.addBodyParameter("buyTime",userInfo.getTrailer_buy_time()+"");
        params.addBodyParameter("serviceLife",userInfo.getTrailer_service_life());
        params.addBodyParameter("licenseImg",userInfo.getTrailer_license_img());
        if (!StringUtil.isEmpty(userInfo.moveCardA))
            params.addBodyParameter("file",new File(userInfo.moveCardA));
        params.addBodyParameter("driverId",userInfo.getId()+"");
        if (userInfo.getTrailerId()>0)
            params.addBodyParameter("id",userInfo.getTrailerId()+"");
        params.addBodyParameter("showLoading", AbsServicexUtils.showAndHideLoading);
        getObjectFromServer(params,1);
    }

    private void orcIdcard(String imgPath) {
        requestType=3;
        RequestParams params = null;
        params = new RequestParams("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard");
        params.addHeader("Content-Type", "application/x-www-form-urlencoded");
        params.setMultipart(true);
        params.addBodyParameter("access_token", BD_token);
        String afterBase64 = ImageUtil.bitmapToString(imgPath);
        params.addBodyParameter("image", afterBase64);
        params.addBodyParameter("detect_direction", "true");
        params.addBodyParameter("id_card_side", "front");
        getObjectFromServer(params,1);
    }


}
